package crmdna.payment;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.*;

import paypalnvp.fields.*;
import paypalnvp.request.*;

@SuppressWarnings("serial")
public class SetExpressCheckoutServlet extends HttpServlet {
	
	public void doOptions(HttpServletRequest req, HttpServletResponse resp)
	        throws IOException {
	    resp.setHeader("Access-Control-Allow-Origin", "*");
	    resp.setHeader("Access-Control-Allow-Methods", "POST");
	    resp.setHeader("Access-Control-Allow-Headers", "Content-Type");
	    resp.setHeader("Access-Control-Max-Age", "86400");
	    resp.setHeader("Allow", "POST, OPTIONS");
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		HttpSession session = req.getSession();
		session.setAttribute("firstName", req.getParameter("firstName"));
		session.setAttribute("lastName", req.getParameter("lastName"));
		session.setAttribute("nickName", req.getParameter("nickName"));
		session.setAttribute("gender", req.getParameter("gender"));
        session.setAttribute("email", req.getParameter("email"));
        session.setAttribute("postalCode", req.getParameter("postalCode"));
        session.setAttribute("country", req.getParameter("country"));
        session.setAttribute("mobilePhone", req.getParameter("mobilePhone"));
        session.setAttribute("homePhone", req.getParameter("homePhone"));
        session.setAttribute("survey", req.getParameter("survey"));
        session.setAttribute("programId", req.getParameter("programId"));
        session.setAttribute("fee", req.getParameter("fee"));
        session.setAttribute("returnUrl", req.getParameter("returnUrl"));
        session.setAttribute("cancelUrl", req.getParameter("cancelUrl"));
        session.setAttribute("errorUrl", req.getParameter("errorUrl"));
        session.setAttribute("vendor", req.getParameter("vendor"));
        
        resp.setHeader("Access-Control-Allow-Origin", "*");
		resp.setContentType("application/json");
		PrintWriter pw = resp.getWriter();
		pw.print(requestPayment(
				req.getParameter("vendor"),
				req.getParameter("email"),
				req.getParameter("fee"),
				req.getParameter("programId"), 
				req.getParameter("returnUrl"), 
				req.getParameter("cancelUrl")));
		pw.close();
	}
	
	private String requestPayment(String vendor, String email, String fee, String programId, String returnUrl, String cancelUrl) {

        /* FIXME: Get these from DB */
        String title = "Surya Kriya"; 
        String type = "SuryaKriya";
        String dates = "March 8 & 9, 2014";
        long regId = System.currentTimeMillis();
        
		PaymentItem item = new PaymentItem();
        item.setAmount(fee + ".00");
        item.setName(title + " - " + dates);

        PaymentItem[] items = { item };
		Payment payment = new Payment(items);
		VendorPayment vp = new VendorPayment(vendor);
		
		payment.setInvoiceNumber(type + "-" + programId + "-" + regId);
		payment.setCurrency(vp.currency);
		
		SetExpressCheckout setEC = new SetExpressCheckout(payment, returnUrl, cancelUrl);
		
		//setEC.setImage(imgUrl);
		setEC.setEmail(email);
		vp.paypal.setResponse(setEC);
		Map<String, String> response = setEC.getNVPResponse();
		System.out.println("SEC: " + response.toString());
		
		if ("Success".equals(response.get("ACK"))) {
			Response crsp = new Response(response.get("ACK"));
			return crsp.get(response.get("TOKEN"), vp.env);
		} else {
			Response crsp = new Response(response.get("ACK"));
			return crsp.get(response.get("L_SEVERITYCODE0"), response.get("L_ERRORCODE0"), 
					response.get("L_SHORTMESSAGE0"), response.get("L_LONGMESSAGE0"));
	    }
	}
}
